# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
"""
Casket Stacks
"""

from ..utillib.misc import kwdefault
from .. import pilelib as stack
#from .. import helpstrings as hs
from .. import comparecards as cmp_cards
from ..packlib import info as cs


class Foundation(stack.Vary_SS_Foundation):

    def __init__(self, x, y, game, suit, **kw):
        """
        __init__(x:int, y:int, game:obj, suit:int[, offset:tup=(0, 0),
          *capability* {dir=0, base_rank=ANY, max_move=0}]) -> None

        :param integer x: left x-coordinate.
        :param integer y: top y-coordinate.
        :param object game: current Game.
        :param integer suit: pile's suit and base_suit
        :keyword tuple offset: offset from left and offset from top

        note:: modified max_cards *capability* to size itself accoring to
          Game['suits'].

        seealso::
          Module pysollib.stackcaps with details of *capability* keywords.
        """
        kwdefault(kw, dir=0, base_rank=cs.ANY, max_move=0)
        super(Foundation, self).__init__(x, y, game, suit, **kw)

    def cardsBuild(self, from_stack, cards):
        """
        cardsBuild(from_stack:obj, cards:seq) -> bool

        Check parent.cardsBuild() and cards isSequence() with top card
        acccording to *capabilities*. Set cap['dir'] for all piles when pile has
        cards and cap['dir'] not set

        :param object from_stack: sending pile
        :param sequence cards: cards from sending pile
        :return: pile accepts ``cards``?
        :rtype: boolean
        """
        if self.cap['dir'] == 0 and self.cards:
            self.game._save_stack_caps(self.game.s['foundations'],
                          dir=cmp_cards.getDirection(self.cards[-1:] + cards))
        return super(Foundation, self).cardsBuild(from_stack, cards)


class RowStack(stack.NotFromReserveBuildMethod, stack.ReserveStack):
    _BOTTOM_IMAGE_ = 'shade'

    @property
    def _fromstacks(self):
        """
        Piles cards may not be played from Game.s.reserves or Game.s.braid.

        :param object from_stack: pile that held sequence.
        :param sequence cards: sequence.
        :return: sequence accepted?
        :rtype: boolean
        """
        return [self.game.s['braid']] + super(RowStack, self)._fromstacks()


class BackboneStack(stack.OpenStack):

    def cardsBlock(self):
        return not self.game.s['reserves'][-1].cards


class Reserve(stack.ReserveStack):
    _BOTTOM_IMAGE_ = 'braid'